%%% Calculate Exogenous Growth Gains
clear
rng(17)

results = [];

%%% THE PARAMETERS BELOW ARE SET UP TO SHOW THE RESULTS RELATIVE TO IDEAS
%%% AUTARKY - THE RESULTS RELATIVE TO TRADE AUTARKY ARE ACHIEVABLE BY
%%% COMMENTING DIFFERENT LINES BELOW SUCH THAT THE z VALUES ARE CONSTANT AND THE
%%% IMPORT TARIFF CHANGES IN THE COUNTERFACTUAL
params = [0.1207	0.0296	0.1584	1.538	7.108	0.938];
max_year = 1000;
% z_US_entry = 0.147;
% z_OECD_entry = 0.078;
z_US_entry = 0.01;
z_OECD_entry = 0.01;
z_US_counterfactual = 0.147;
z_OECD_counterfactual = 0.078;
%import_tariff = 4;
import_tariff = 1.538
import_tariff_counterfactual = 1.538; 
beta = 1.03/1.05;

wage_gains_tracker_US = 0;
wage_gains_tracker_OECD = 0;
consumption_gains_tracker_US = 0;
consumption_gains_tracker_OECD = 0;

for repeats = 1:1:10
[wage_levels_counterfactual, real_consumption] = steady_state_simulation_exogenous_gains_targeted_disembodied(params, max_year, ...
                                            import_tariff, import_tariff_counterfactual, z_US_entry, z_OECD_entry, z_US_counterfactual, z_OECD_counterfactual);

wage_growth_US = geomean(wage_levels_counterfactual(201:299,1)./wage_levels_counterfactual(200:298,1));
wage_growth_OECD = geomean(wage_levels_counterfactual(201:299,2)./wage_levels_counterfactual(200:298,2));
consumption_growth_US = geomean(real_consumption(201:299,1)./real_consumption(200:298,1));
consumption_growth_OECD = geomean(real_consumption(201:299,2)./real_consumption(200:298,2));

baseline_growth = mean([wage_growth_US,wage_growth_OECD,consumption_growth_US,consumption_growth_OECD]);


wage_levels_counterfactual_US = real_consumption(:,1);
wage_levels_counterfactual_OECD = real_consumption(:,2);

wage_levels_baseline_US = real_consumption(1:299,1);
counter = 1;
while counter <= max_year-299
    if import_tariff_counterfactual<1.474
wage_levels_baseline_US(end+1) = wage_levels_baseline_US(end)*1.0301;
    else
wage_levels_baseline_US(end+1) = wage_levels_baseline_US(end)*baseline_growth;
    end
counter = counter+1;
end

wage_levels_baseline_OECD = real_consumption(1:299,2);
counter = 1;
while counter <= max_year-299
    if import_tariff_counterfactual<1.474
wage_levels_baseline_OECD(end+1) = wage_levels_baseline_OECD(end)*1.0301;
    else
wage_levels_baseline_OECD(end+1) = wage_levels_baseline_OECD(end)*baseline_growth;
    end
counter = counter+1;
end

discount_power_vec = 1:max_year-299;
discount_factor_vec = beta.^(discount_power_vec-1)';

PDU_baseline_US = sum(discount_factor_vec.*(log(wage_levels_baseline_US(300:end))));
PDU_counterfactual_US = sum(discount_factor_vec.*(log(wage_levels_counterfactual_US(300:end))));

PDU_baseline_US_temp = PDU_baseline_US;
wage_levels_baseline_US_temp = wage_levels_baseline_US(300:end);
US_EV_multiplier = 1;
while PDU_baseline_US_temp < PDU_counterfactual_US
    US_EV_multiplier = US_EV_multiplier+0.0001;
    wage_levels_baseline_US_temp = wage_levels_baseline_US(300:end).*US_EV_multiplier;
    PDU_baseline_US_temp = sum(discount_factor_vec.*(log(wage_levels_baseline_US_temp)));
end

PDU_baseline_OECD = sum(discount_factor_vec.*(log(wage_levels_baseline_OECD(300:end))));
PDU_counterfactual_OECD = sum(discount_factor_vec.*(log(wage_levels_counterfactual_OECD(300:end))));

PDU_baseline_OECD_temp = PDU_baseline_OECD;
wage_levels_baseline_OECD_temp = wage_levels_baseline_OECD(300:end);
OECD_EV_multiplier = 1;
while PDU_baseline_OECD_temp < PDU_counterfactual_OECD
    OECD_EV_multiplier = OECD_EV_multiplier+0.0001;
    wage_levels_baseline_OECD_temp = wage_levels_baseline_OECD(300:end).*OECD_EV_multiplier;
    PDU_baseline_OECD_temp = sum(discount_factor_vec.*(log(wage_levels_baseline_OECD_temp)));
end

dynamic_gains_US_consumption = US_EV_multiplier
dynamic_gains_OECD_consumption = OECD_EV_multiplier

% wage_gains_tracker_US = wage_gains_tracker_US+dynamic_gains_US_wages;
% wage_gains_tracker_OECD = wage_gains_tracker_OECD+dynamic_gains_OECD_wages;

consumption_gains_tracker_US = consumption_gains_tracker_US+dynamic_gains_US_consumption;
consumption_gains_tracker_OECD = consumption_gains_tracker_OECD+dynamic_gains_OECD_consumption;


end

% wage_gains_US = wage_gains_tracker_US./repeats
% wage_gains_OECD = wage_gains_tracker_OECD./repeats
consumption_gains_US = consumption_gains_tracker_US./repeats
consumption_gains_OECD = consumption_gains_tracker_OECD./repeats

results = [results; consumption_gains_US consumption_gains_OECD];

%save('exogenous_gains_near_autarky_disembodied.mat','results');
save('exogenous_gains_ideas_autarky_disembodied.mat','results');


